package com.lucky.ebidding.controller;

import com.lucky.ebidding.dto.NotificationDTO;
import com.lucky.ebidding.dto.ResultDTO;
import com.lucky.ebidding.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ClassName:notificationController
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/4/25 15:20
 * @Author:857251389@qq.com
 */
@Controller
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

//    @GetMapping("/notification/{userId}")
//    public String notification(@PathVariable(name = "userId") Integer userId){
//
//        return "myNotification";
//    }

    @GetMapping("/notification/{receiver}/{status}")
    public String unRead(@PathVariable(name = "receiver") Integer receiver,
                         @PathVariable(name = "status") Integer status,
                         Model model){

        List<NotificationDTO> notificationDTOs = notificationService.selectNotificationByReceiver(receiver,status);


        model.addAttribute("unReadNotifications",notificationDTOs);
        return "unRead";
    }

    @ResponseBody
    @GetMapping("/notification/{id}")
    public ResultDTO setNotificationId(@PathVariable(name = "id") Integer id){

        int i = notificationService.setStatusRead(id);
        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }


}
